package adapter;

import java.util.ArrayList;
import java.util.List;

import lfm.mpr.R;
import objects.Equipo;
import objects.Jugador;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class ExpandableAdapterListJugadores extends BaseExpandableListAdapter {
 
    	private ArrayList<String> groups;
    	private ArrayList<Jugador> list_jugadores_select;
        private ArrayList<ArrayList<ArrayList<Jugador>>> children;
    	private Context context;
    	private Equipo team;
    	private boolean[] itemSelection_portero;
    	private boolean[] itemSelection_defensas;
    	private position_list[] position_list_defensas;
    	private position_list[] position_list_medios;
    	private position_list[] position_list_delanteros;
    	private boolean[] itemSelection_medios;
    	private boolean[] itemSelection_delanteros;
    	private boolean[] itemGroupSelection;
    	
    	View view;
    	LayoutInflater inflater;
    	static ViewHolder holder;


 
    	public ExpandableAdapterListJugadores (Context context, ArrayList<String> groups, 
    			ArrayList<ArrayList<ArrayList<Jugador>>> children, Equipo team) {
            this.context = context;
            this.groups = groups;
            this.children = children;
            this.team = team;
            this.itemSelection_portero = new boolean[children.size()];
            this.itemGroupSelection = new boolean[groups.size()];
            this.itemSelection_defensas = new boolean[children.size()];
            this.itemSelection_medios = new boolean[children.size()];
            this.itemSelection_delanteros = new boolean[children.size()];
            this.position_list_defensas = new position_list[children.size()];
            this.position_list_medios = new position_list[children.size()];
            this.position_list_delanteros = new position_list[children.size()];
        }
    	
    	static class ViewHolder {
            protected TextView nombre, precio, equipo;
            protected CheckBox checkbox;
        }
    	
    	static class position_list{
    		protected int position;
            protected boolean check;
    	}
 
 
        public boolean areAllItemsEnabled(){
            return true;
        }
 
        public ArrayList<Jugador> getChild(int groupPosition, int childPosition) {
            return children.get(groupPosition).get(childPosition);
        }
 
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
 
 
        public View getChildView(final int groupPosition, final int childPosition, 
        		boolean isLastChild, View convertView, ViewGroup parent) {
        	
        	view = null;
        	Jugador hijo = (Jugador) ((ArrayList<Jugador>)getChild(groupPosition, childPosition)).get(0);
        	final ViewHolder viewHolder;

			if (convertView == null) {
				viewHolder = new ViewHolder();
				inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.rowbuttonlayout, null);
                
                viewHolder.nombre = (TextView) view.findViewById(R.id.text_lst_name);
                viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
                viewHolder.precio = (TextView) view.findViewById(R.id.text_lst_price);
                viewHolder.equipo = (TextView) view.findViewById(R.id.text_lst_team);
                
                view.setTag(viewHolder);
                viewHolder.checkbox.setTag(children.get(childPosition));
                viewHolder.checkbox.setId(childPosition);

                Log.i("LFM_Log","convertView es nullo ");
			}
			else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
				//viewHolder = (ViewHolder) convertView.getTag();
				view = convertView;
	            ((ViewHolder) view.getTag()).checkbox.setTag(children.get(childPosition));
				Log.i("LFM_Log","convertView no es nullo ");
            }
			
			
			holder = (ViewHolder) view.getTag();
	        holder.nombre.setText(hijo.getNombre());
	        holder.precio.setText(String.valueOf(hijo.getPrecio()));
	        holder.equipo.setText("kk");


	        return view;

	
			/*if(groupPosition == 0) viewHolder.checkbox.setChecked(itemSelection_portero[childPosition]);
			else if(groupPosition == 1) viewHolder.checkbox.setChecked(itemSelection_defensas[childPosition]);
			else if(groupPosition == 2) viewHolder.checkbox.setChecked(itemSelection_medios[childPosition]);
			else if(groupPosition == 3) viewHolder.checkbox.setChecked(itemSelection_delanteros[childPosition]);
			
			convertView.setTag(viewHolder);

			viewHolder.nombre.setText(hijo.getNombre());
			viewHolder.precio.setText(String.valueOf(hijo.getPrecio()));
			viewHolder.equipo.setText(hijo.getEquipo());*/
			
			

			 
			
            //return convertView;
        }//end_getChildView
 
        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }
        
       
        public String getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }
 
       
        public int getGroupCount() {
            return groups.size();
        }
 
       
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
 
        
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
 
        	String group = (String) getGroup(groupPosition);

        	if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_grupo, null);
            }
            TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGrupo);
            grouptxt.setText("     " + group.substring(0, 1).toUpperCase() + group.substring(1).toLowerCase() + "s");

            return convertView;
        }
 
        
        public boolean hasStableIds() {
            return true;
        }
 
        
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }
        
        public void mostrar_alert(String title, String message){
        	
        	    AlertDialog.Builder builder = new AlertDialog.Builder(context);
			    builder.setTitle(title);
			    builder.setMessage(message);
			    builder.setPositiveButton("OK", new OnClickListener() {
			        public void onClick(DialogInterface dialog, int which) {
			            dialog.cancel();
			        }
			    });
			  
			    builder.create().show(); 
        	
        }
 
    }
